package com.zy.core.util.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.JSONLibDataFormatSerializer;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.List;
import java.util.Map;

/**
 * Package: core.util.json
 * <p>
 * Description： TODO
 * <p>
 * Author: zhangyu
 * <p>
 * Date: Created in 2018-10-5 21:53
 * <p>
 * Company: mingtu
 * <p>
 * Copyright: Copyright (c) 2018
 * <p>
 * Version: 0.0.1
 * <p>
 * Modified By:
 */
public class FastJsonUtils {
    private static final SerializeConfig config;

    static {
        config = new SerializeConfig();
        config.put(java.util.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
        config.put(java.sql.Date.class, new JSONLibDataFormatSerializer()); // 使用和json-lib兼容的日期输出格式
    }

    private static final SerializerFeature[] features = {SerializerFeature.WriteMapNullValue, // 输出空置字段
            SerializerFeature.WriteNullListAsEmpty, // list字段如果为null，输出为[]，而不是null
            SerializerFeature.WriteNullNumberAsZero, // 数值字段如果为null，输出为0，而不是null
            SerializerFeature.WriteNullBooleanAsFalse, // Boolean字段如果为null，输出为false，而不是null
            SerializerFeature.WriteNullStringAsEmpty // 字符类型字段如果为null，输出为""，而不是null
    };

    /**
     * 功能描述：把JSON数据转换成普通字符串列表
     * @Title: getStringList
     * @Description: TODO
     * @param jsonData
     * @return
     * @throws Exception List<String>
     * @throws
     */
    public static List<String> getStringList(String jsonData) throws Exception {
        return JSON.parseArray(jsonData, String.class);
    }

    /**
     * 功能描述：把JSON数据转换成指定的java对象
     * @Title: getSingleBean
     * @Description: TODO
     * @param jsonData
     * @param clazz
     * @return
     * @throws Exception T
     * @throws
     */
    public static <T> T getSingleBean(String jsonData, Class<T> clazz)
            throws Exception {
        return JSON.parseObject(jsonData, clazz);
    }

    /**
     * 功能描述：把JSON数据转换成指定的java对象列表
     * @Title: getBeanList
     * @Description: TODO
     * @param jsonData
     * @param clazz
     * @return
     * @throws Exception List<T>
     * @throws
     */
    public static <T> List<T> getBeanList(String jsonData, Class<T> clazz)
            throws Exception {
        return JSON.parseArray(jsonData, clazz);
    }

    /**
     * 功能描述：把JSON数据转换成较为复杂的java对象列表
     * @Title: getBeanMapList
     * @Description: TODO
     * @param jsonData
     * @return
     * @throws Exception List<Map<String,Object>>
     * @throws
     */
    public static List<Map<String, Object>> getBeanMapList(String jsonData)
            throws Exception {
        return JSON.parseObject(jsonData,
                new TypeReference<List<Map<String, Object>>>() {
                });
    }

    /**
     * 将javaBean输出为json格式
     * @Title: toJSONString
     * @Description: TODO
     * @param object
     * @return String
     * @throws
     */
    public static String toJSONString(Object object) {
        return JSON.toJSONString(object, config, features);
    }

    /**
     * 将javaBean输出为json格式
     * @Title: toJSONString
     * @Description: TODO
     * @param object
     * @param filter 过滤json中不需要的字段
     * @return String
     * @throws
     */
    public static String toJSONString(Object object, ComplexPropertyPreFilter filter) {
        if(filter==null){
            return JSON.toJSONString(object, config, features);
        }
        return JSON.toJSONString(object, config,filter, features);
    }

    public static String toJSONNoFeatures(Object object) {
        return JSON.toJSONString(object, config);
    }

    public static String toJSONNoFeatures(Object object,ComplexPropertyPreFilter filter) {
        if(filter==null){
            return JSON.toJSONString(object, config);
        }
        return JSON.toJSONString(object, config, filter);
    }

    /**
     * 转换为数组
     * @Title: toArray
     * @Description: TODO
     * @param text
     * @return Object[]
     * @throws
     */
    public static <T> Object[] toArray(String text) {
        return toArray(text, null);
    }

    /**
     * 转换为数组
     * @Title: toArray
     * @Description: TODO
     * @param text
     * @param clazz
     * @return Object[]
     * @throws
     */
    public static <T> Object[] toArray(String text, Class<T> clazz) {
        return JSON.parseArray(text, clazz).toArray();
    }

    /**
     * 将string转化为序列化的json字符串
     * @Title: textToJson
     * @Description: TODO
     * @param text
     * @return Object
     * @throws
     */
    public static Object textToJson(String text) {
        Object objectJson  = JSON.parse(text);
        return objectJson;
    }
}
